package com.wanshi.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanshi.cloud.client.ProductClient;
import com.wanshi.cloud.entity.Orderitem;
import com.wanshi.cloud.entity.Orders;
import com.wanshi.cloud.entity.Products;
import com.wanshi.cloud.mapper.OrderMapper;
import com.wanshi.cloud.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanshi.cloud.utils.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-09-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductClient productClient;

    @Override
    public List<Orders> findByUserId(Integer userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        return this.list(wrapper);
    }


    @Override
    public Orders findById(String id) {
        return this.getById(id);
    }

    @Override
    public Orders findProductById(String id) {
        Orders orders = orderMapper.findProductById(id);
        List<Orderitem> orderitemList= orders.getOrderitem();

        for (int i = 0; i <orderitemList.size() ; i++) {
            Orderitem orderitem = orderitemList.get(i);
            Integer prodId = orderitem.getProdId();
            Products byId = productClient.findById(prodId);

            orderitem.setProducts(byId);
        }

        return orders;
    }
}
